package com.wang.tool.monitor.weaver;

import com.wang.tool.monitor.LinkLogUtil;
import com.wang.tool.monitor.core.MonitorInfo;
import com.wang.tool.monitor.template.AbsMonitorInvoker;

/**
 * 类似于
 *
 * @author shaopeng
 * @date 2023/5/14
 * @see BaseAnnotationWeaver
 * @see AbsMonitorInvoker
 */
public class BaseMonitorWeaver<R> implements MonitorWeaver<R> {


    @Override
    public void weaveOnBefore(MonitorInfo<R> monitorInfo) throws Throwable {
    }

    @Override
    public final void weaveOnAfter(MonitorInfo<R> monitorInfo) throws Throwable {
        internalAfter(monitorInfo);
        String expectStatus = getExpectStatus();
        boolean notExpectStatus = expectStatus != null && !expectStatus.equals(monitorInfo.getStatus());
        if (notExpectStatus) {
            LinkLogUtil.newTagLog(MonitorInfo.TAG_NOT_EXPECT_STATUS)
                    .v1(expectStatus)
                    .v2(monitorInfo.getStatus())
                    .pointLink(monitorInfo.getLink())
                    .log();
        }
        boolean special = monitorInfo.isFailOut() || monitorInfo.isImplicitError() || notExpectStatus || monitorInfo.isDebug();

        //恒输出入参/错/隐性异常/慢/debug/非预期状态
        if (isPrintParam() || special || monitorInfo.isSlow()) {
            Object param = fetchParam(monitorInfo.getParams());
            if (param != null) {
                //载荷中记录入参
                monitorInfo.addDetailParam(param);
//                monitorInfo.addShortDetailOnDebug(MonitorInfo.PARAM, param);
            }
        }

        //载荷中记录结果
        if (isPrintResult() || isSpecialPrintResult() && special) {
            Object fetchResult = fetchResult(monitorInfo.getResult());
            if (fetchResult != null) {
                monitorInfo.addDetailResult(fetchResult);
                //链路上不记录结果,太大了
//                monitorInfo.addShortDetailOnDebug(MonitorInfo.RESULT, "结果详情请查看日志");
            }
        }
    }

    /**
     * 是不是恒输出入参
     *
     * @return
     */
    protected boolean isPrintParam() {
        return false;
    }


    /**
     * 是不是恒输出出参
     *
     * @return
     */
    protected boolean isPrintResult() {
        return false;
    }


    /**
     * 预期的状态码 可为空,不判断该逻辑
     * expectStatus
     * 结果码和预期不一致会在details中输出入参
     *
     * @return
     */
    protected String getExpectStatus() {
        return null;
    }


    /**
     * 特殊情况在details中输出结果
     * 超时 非预期status debug
     * specialDetailResult
     *
     * @return
     */
    protected boolean isSpecialPrintResult() {
        return false;
    }

    /**
     * 提取参数
     *
     * @param params 可为空
     * @return
     */
    protected Object fetchParam(Object[] params) {
        return params == null || params.length == 0 ? null : params.length == 1 ? params[0] : params;
    }

    /**
     * 提取结果
     * 下游可重写
     *
     * @param result 可为空
     * @return
     */
    protected Object fetchResult(R result) {
        return result;
    }

    /**
     * 更新状态等
     *
     * @param monitorInfo 非空
     */
    protected void internalAfter(MonitorInfo<R> monitorInfo) throws Throwable {
    }
}
